<?php


namespace App\Common;


use App\Utils\DMSQL;
use App\Utils\Logger;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class DBLog implements Logger
{
    private $db = null;


    public function __construct()
    {
        $this->db = new DMSQL();
    }

    public function __destruct()
    {
        // TODO: Implement __destruct() method.
        $this->db = null;
    }

    /**
     * 用户行为日志
     * @param int $type 1-添加，2-修改，3-删除
     * @param string $content 内容
     * @return mixed
     */
    public function addLog(string $content,int $type=1,int $status=1)
    {
        // TODO: Implement addLog() method.
        $data['uid'] = authcode(session('uid'));
        $data['username'] = authcode(session('username'));
        $data['ipaddress'] = request()->ip();
        $data['content'] = $content;
        $data['created_at'] = time();
        $data['type'] = $type;
        $data['status'] = $status;
        if($status == 2){
            $data['params'] = json_encode(request()->all());
        }else{
            $data['params'] = '';
        }
        $result = $this->db->table('user_log')->insert($data);
        $this->fileLog($data);
        if($result == 10001){
            Log::info($result['msg']);
        }
    }

    /**
     * 用户修改日志
     * @param array $oldArr 旧数据
     * @param array $newArr 新数据
     * @param string $model 位置
     */
    public function editLog(array $oldArr,array $newArr,string $model,int $status=1)
    {
        $content = "";
        // TODO: Implement editLog() method.
        foreach ($oldArr as $key=>$value){
            if(array_key_exists($key,$newArr)){
                if($value != $newArr[$key]){
                    $content .= "字段{$key}的值从【{$value}】改变为了【{$newArr[$key]}】;";
                }
            }
        }
        $data['uid'] = authcode(session('uid'));
        $data['username'] = authcode(session('username'));
        $data['ipaddress'] = request()->ip();
        $data['content'] = "用户在{$model}编辑了ID为{$oldArr['id']}的数据------{$content}";
        $data['created_at'] = time();
        $data['type'] = 2;
        $data['status'] = $status;
        if($status == 2){
            $data['params'] = json_encode(request()->all());
        }else{
            $data['params'] = '';
        }
        $result = $this->db->table('user_log')->insert($data);
        $this->fileLog($data);
        if($result == 10001){
            Log::info($result['msg']);
        }
    }

    /**
     * 删除日志
     * @param string $model
     * @param int $ID
     */
    public function delLog(string $model, $ID,int $status=1)
    {
        // TODO: Implement delLog() method.
        $data['uid'] = authcode(session('uid'));
        $data['username'] = authcode(session('username'));
        $data['ipaddress'] = request()->ip();
        $data['content'] = "用户在{$model}删除了ID为{$ID}的数据";
        $data['created_at'] = time();
        $data['type'] = 3;
        $data['status'] = $status;
        if($status == 2){
            $data['params'] = json_encode(request()->all());
        }else{
            $data['params'] = '';
        }
        $result = $this->db->table('user_log')->insert($data);
        $this->fileLog($data);
        if($result == 10001){
            Log::info($result['msg']);
        }
    }

    public function fileLog(array $data)
    {
        $content = "";
        $content .= date("Y-m-d H:i:s",$data['created_at']) . "     ";
        $content .= $data['uid'] . "        ";
        $content .= $data['username'] . "       ";
        $content .= $data['ipaddress'] . "      ";
        $content .= $data['content'] . "        ";
        $content .= $data['type'] . "       ";
        $content .= $data['status']."       ";
        $content .= $data['params'];
        $file = date("Ymd",time()).".log";
        Storage::disk('filelog')->append($file,$content);
        Log::info($content);
    }
}
